﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.IO;
using System.Collections;
using System.Data.SqlClient;
using Newtonsoft.Json;
using CoinDev.Areas.UserAdministration.Models.JSON;
using System.Data;

namespace CoinDev.Areas.UserAdministration.Controllers
{
    public class ItemController : Controller
    {
        //
        // GET: /UserAdministration/Item/

        public ActionResult Index()
        {
            return View("List");
        }

        public ActionResult Add()
        {
            ArrayList Params = new ArrayList
            {
            };

            var dtCategories = Data.Connections.GetDataTable("Admin_Get_CategoryListDDL", Params, "ApplicationServices");

            var CategoryDDL = new List<SelectListItem>();

            CategoryDDL.Add(new SelectListItem { Text = "- Select -", Value = "" });

            foreach (DataRow drCategories in dtCategories.Rows)
            {
                CategoryDDL.Add(new SelectListItem { Text = drCategories["Name"].ToString(), Value = drCategories["Id"].ToString() });
            }

            ViewData["CategoryDDL"] = CategoryDDL;

            return View("Add");
        }
        public ActionResult Edit()
        {
            try
            {
                ArrayList paramList = new ArrayList
                {
                    new SqlParameter("Id", Request.Params["Id"])
                };

                var dtItemInfo = CoinDev.Data.Connections.GetDataTable("Admin_Get_ItemById", paramList, "ApplicationServices");

                var Items = new Models.JSON.ItemsArray { };

                foreach (DataRow drItemInfo in dtItemInfo.Rows)
                {
                    Items.Item.Add(new Models.JSON.Items
                    {
                        Id = drItemInfo["Id"].ToString(),
                        CategoryId = drItemInfo["CategoryId"].ToString(),
                        Name = drItemInfo["Name"].ToString(),
                        Description = drItemInfo["Description"].ToString(),
                        Year = drItemInfo["Year"].ToString(),
                        Price = drItemInfo["Price"].ToString(),
                        PictureLocation = drItemInfo["Picture"].ToString()
                    });
                }

                ArrayList Params = new ArrayList
                {
                };

                var dtCategories = Data.Connections.GetDataTable("Admin_Get_CategoryListDDL", Params, "ApplicationServices");

                var CategoryDDL = new List<SelectListItem>();

                CategoryDDL.Add(new SelectListItem { Text = "- Select -", Value = "" });

                foreach (DataRow drCategories in dtCategories.Rows)
                {
                    CategoryDDL.Add(new SelectListItem { Text = drCategories["Name"].ToString(), Value = drCategories["Id"].ToString() });
                }

                ViewData["CategoryDDL"] = CategoryDDL;
                
                return View("Edit", Items);
            }
            catch
            {
                var Check = new Models.JSON.SubmissionCheck
                {
                    Check = "0",
                    Message = "No Item",
                };
                return View("Edit");
            }
        }

        public ActionResult NewItem()
        {
            var sr = new StreamReader(Request.InputStream);
            var contentString = sr.ReadToEnd();
            var parameterobjects = JsonConvert.DeserializeObject<Items>(contentString);
            var PictureLocation = "../../Content/Images/Item/";
            var message = "";
            var check = "1";

            if (parameterobjects.PictureLocation == "")
                PictureLocation = "";
            else
                PictureLocation = PictureLocation + "" + parameterobjects.PictureLocation;

            ArrayList paramList = new ArrayList
            {
                new SqlParameter("CategoryId", parameterobjects.CategoryId),
                new SqlParameter("Name", parameterobjects.Name),
                new SqlParameter("Year", parameterobjects.Year),
                new SqlParameter("Description", parameterobjects.Description),
                new SqlParameter("Price", parameterobjects.Price),
                new SqlParameter("Picture", PictureLocation)
            };

            var drInsertCommissionAdjustment = CoinDev.Data.Connections.GetDataRow("Admin_Insert_Item", paramList, "ApplicationServices");

            Session["Id"] = Convert.ToInt32(drInsertCommissionAdjustment["Id"].ToString());
            message = drInsertCommissionAdjustment["message"].ToString();
            check = drInsertCommissionAdjustment["check"].ToString();
            Session["New"] = 1;

            var SubmissionCheck = new Models.JSON.SubmissionCheck
            {
                Check = check,
                Message = message
            };

            return Json(SubmissionCheck, JsonRequestBehavior.AllowGet);
            
        }
        public ActionResult EditItem()
        {
            var sr = new StreamReader(Request.InputStream);
            var contentString = sr.ReadToEnd();
            var parameterobjects = JsonConvert.DeserializeObject<Items>(contentString);
            var PictureLocation = "../../Content/Images/Item/";
            var message = "";
            var check = "1";

            if (parameterobjects.PictureLocation == "")
                PictureLocation = "";
            else
                PictureLocation = PictureLocation + "" + parameterobjects.PictureLocation;

            ArrayList paramList = new ArrayList
            {
                new SqlParameter("Id", parameterobjects.CategoryId),
                new SqlParameter("CategoryId", parameterobjects.CategoryId),
                new SqlParameter("Name", parameterobjects.Name),
                new SqlParameter("Year", parameterobjects.Year),
                new SqlParameter("Description", parameterobjects.Description),
                new SqlParameter("Price", parameterobjects.Price),
                new SqlParameter("Picture", PictureLocation)
            };

            var drInsertCommissionAdjustment = CoinDev.Data.Connections.GetDataRow("Admin_Edit_Item", paramList, "ApplicationServices");

            Session["Id"] = Convert.ToInt32(drInsertCommissionAdjustment["Id"].ToString());
            message = drInsertCommissionAdjustment["message"].ToString();
            check = drInsertCommissionAdjustment["check"].ToString();

            var SubmissionCheck = new Models.JSON.SubmissionCheck
            {
                Check = check,
                Message = message
            };

            return Json(SubmissionCheck, JsonRequestBehavior.AllowGet);

        }

        public ActionResult UploadPicture()
        {
            try
            {
                foreach (string file in Request.Files)
                {
                    var postedFile = Request.Files[file];
                    postedFile.SaveAs(Server.MapPath("~/Content/Images/Item/") + Path.GetFileName(postedFile.FileName));
                }
                return Redirect("Edit?Id=" + Convert.ToInt32(Session["Id"]));
            }
            catch
            {
                return Redirect("Add?Id=0");
            }
        }
    }
}
